#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
	int a,b;
	bool friend operator <(node A,node B)
	{
		if(A.a==B.a) return A.b>B.b;//dade
		return A.a>B.a;//xiaode
	}
};
priority_queue <node> qe;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,cnt,ans;
		cnt = 1;
		node stone;
		while(!qe.empty())
		{
			qe.pop();
		}
		scanf("%d",&n);
		while(n--)
		{
			scanf("%d %d",&stone.a,&stone.b);
			qe.push(stone);
		}
		while(!qe.empty())
		{
			if(cnt%2)
			{
				stone = qe.top();
				//printf("1***%d %d\n",stone.a,stone.b);
				stone.a += stone.b;
				qe.pop();
				qe.push(stone);
			}
			else
			{
				stone = qe.top();
				ans = stone.a;
				//printf("2***%d %d\n",stone.a,stone.b);
				qe.pop();
			}
			cnt++;
		}
		printf("%d\n",ans);
	}
}
